const regs = {
  email: /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/,
  number: /^([0]|[1-9][0-9]*)$/,
  password: /^(?=.*\d)(?=.*[a-zA-Z])[\da-zA-Z~!@#$%^&*_]{8,}$/,
  shareCode: /^[A-Za-z0-9]+$/,
};
const verify = (
  rule: { message: string | undefined },
  value: any,
  reg: RegExp,
  callback: (arg0?: Error | undefined) => void
) => {
  if (value) {
    if (reg.test(value)) {
      callback();
    } else {
      callback(new Error(rule.message));
    }
  } else {
    callback();
  }
};

export default {
  email: (
    rule: { message: string | undefined },
    value: any,
    callback: (arg0: Error | undefined) => void
  ) => {
    return verify(rule, value, regs.email, callback);
  },
  number: (
    rule: { message: string | undefined },
    value: any,
    callback: (arg0: Error | undefined) => void
  ) => {
    return verify(rule, value, regs.number, callback);
  },
  password: (
    rule: { message: string | undefined },
    value: any,
    callback: (arg0: Error | undefined) => void
  ) => {
    return verify(rule, value, regs.password, callback);
  },
  shareCode: (
    rule: { message: string | undefined },
    value: any,
    callback: (arg0: Error | undefined) => void
  ) => {
    return verify(rule, value, regs.shareCode, callback);
  },
};
